﻿//-----------------------------------------------------------------------
// <copyright file="AddColumnForm.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionMaker.WinFormApplication
{
    using System;
    using System.Windows.Forms;
    using PivotCollectionMaker.WinFormApplication.Model;
    using PivotCollectionTools.Data;

    /// <summary>
    /// For used to add a column to a category.
    /// </summary>
    public partial class AddColumnForm : Form
    {
        /// <summary>
        /// Storage for the model data.
        /// </summary>
        private TableSchemaModel model;

        /// <summary>
        /// Storage for the selected category.
        /// </summary>
        private PivotTableCategory selectedCategory;

        /// <summary>
        /// Initializes a new instance of the <see cref="AddColumnForm"/> class.
        /// </summary>
        public AddColumnForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model of the add column form.</value>
        public TableSchemaModel Model
        {
            get
            {
                return this.model;
            }

            set
            {
                this.model = value;
                this.BindData();
            }
        }

        /// <summary>
        /// Handles the Click event of the AddCategory control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void AddCategory_Click(object sender, EventArgs e)
        {
            var categoryForm = new PivotTableCategoryForm()
            {
                Model = new PivotTableCategory("New Category", PivotTableCellType.Text)
            };
            if (categoryForm.ShowDialog() == DialogResult.OK)
            {
                this.model.Table.Categories.Add(categoryForm.Model);
                this.selectedCategory = categoryForm.Model;
                this.BindData();
            }
        }

        /// <summary>
        /// Handles the Click event of the Ok control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Ok_Click(object sender, EventArgs e)
        {
            this.model.NewColumn = this.model.Table.AddColumn(this.selectedCategory.Name);
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the CategoryComboBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CategoryComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.uxCategoryComboBox.SelectedIndex >= 0)
            {
                this.selectedCategory = this.model.Table.Categories[this.uxCategoryComboBox.SelectedIndex];
                this.BindSelectedCategoryData();
            }
        }

        /// <summary>
        /// Binds the data.
        /// </summary>
        private void BindData()
        {
            this.uxCategoryList.SuspendBinding();
            this.uxCategoryList.DataSource = this.model.Table.Categories;
            this.uxCategoryList.ResumeBinding();

            if (this.selectedCategory == null || string.IsNullOrEmpty(this.selectedCategory.Name))
            {
                if (this.model.Table.Categories.Count > 0)
                {
                    this.selectedCategory = this.model.Table.Categories[0];
                }
            }

            if (this.selectedCategory != null)
            {
                this.uxCategoryList.Position = this.model.Table.Categories.IndexOf(this.selectedCategory);
                this.BindSelectedCategoryData();
            }
        }

        /// <summary>
        /// Binds the selected category data.
        /// </summary>
        private void BindSelectedCategoryData()
        {
            this.uxCategoryBindingSource.SuspendBinding();
            this.uxCategoryBindingSource.DataSource = this.selectedCategory;
            this.uxCategoryBindingSource.ResumeBinding();

            this.uxColumnName.Text = this.model.Table.GetNextColumnNameForCategory(this.selectedCategory);
        }
    }
}
